import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

const componentMapper = {
  Banner: () => import('@/views/banner/index.vue')
}

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '介绍', icon: 'Menu', affix: true }
      }
    ]
  },
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1/menu1-1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'grid'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index.vue'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        redirect: '/nested/menu1/menu1-1',
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1/index.vue'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2/index.vue'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            redirect: '/nested/menu1/menu1-2/menu1-2-1',
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1/index.vue'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2/index.vue'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3/index.vue'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index.vue'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },
  // 模拟学科管理
  {
    path: '/discipline',
    alwaysShow: true,
    component: Layout,
    meta: { title: '学科管理', icon: 'grid', breadcrumb: false },
    children: [
      {
        path: 'jjx',
        alwaysShow: true,
        meta: { title: '经济学', icon: 'Menu', breadcrumb: false },
        component: () => import('@/views/discipline/index.vue'),
        children: [
          {
            path: 'banner',
            name: 'JjxBanner',
            component: () => import('@/views/banner/index.vue'),
            meta: { title: '轮播图管理', icon: 'Menu', xParams: { xkName: '经济学' } }
          }
        ]
      },
      {
        path: 'zx',
        alwaysShow: true,
        meta: { title: '哲学', icon: 'Menu', breadcrumb: false },
        component: () => import('@/views/discipline/index.vue'),
        children: [
          {
            path: 'banner',
            name: 'ZxBanner',
            component: () => import('@/views/banner/index.vue'),
            meta: { title: '轮播图管理', icon: 'Menu', xParams: { xkName: '哲学' } }
          }
        ]
      }
    ]
  },
  // 模拟学科管理2
  {
    path: '/discipline2',
    alwaysShow: true,
    component: Layout,
    meta: { title: '学科管理2', icon: 'grid', breadcrumb: false },
    children: [
      {
        path: 'banner2',
        name: 'Discipline2Banner2',
        component: componentMapper['Banner'],
        meta: { title: '轮播图管理', icon: 'Menu', xParams: { xkName: '经济学' } }
      }
    ]
  },
  // api-dashboard
  {
    path: '/api-dashboard',
    component: Layout,
    redirect: '/api-dashboard',
    children: [
      {
        path: '',
        name: 'ApiDashboard',
        component: () => import('@/views/api-dashboard/index.vue'),
        meta: { title: 'api大全', icon: 'Menu', affix: true }
      }
    ]
  },
  {
    path: '/external-link',
    component: Layout,
    children: [
      {
        path: 'https://cnki.net/',
        meta: { title: '外部链接', icon: 'link' }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    hidden: true
  }
]

/**
 * 动态路由
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/:catchAll(.*)',
    redirect: '/404',
    name: 'unMatch',
    hidden: true
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes: constantRoutes as RouteRecordRaw[]
})

export default router
